package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("DiscoverylinkDAO")
@Transactional
public class DiscoverylinkDAOImpl extends AbstractJpaDao<Discoverylink> implements DiscoverylinkDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Discoverylink.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public DiscoverylinkDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Discoverylink findDiscoverylinkById(Integer id) throws DataAccessException {

		return findDiscoverylinkById(id, -1, -1);
	}

	@Transactional
	public Discoverylink findDiscoverylinkById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDiscoverylinkById", startResult, maxRows, id);
			return (Discoverylink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Discoverylink> findDiscoverylinkByName(String name) throws DataAccessException {

		return findDiscoverylinkByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Discoverylink> findDiscoverylinkByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDiscoverylinkByName", startResult, maxRows, name);
		return new LinkedHashSet<Discoverylink>(query.getResultList());
	}

	@Transactional
	public Set<Discoverylink> findDiscoverylinkByNameContaining(String name) throws DataAccessException {

		return findDiscoverylinkByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Discoverylink> findDiscoverylinkByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDiscoverylinkByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Discoverylink>(query.getResultList());
	}

	@Transactional
	public Set<Discoverylink> findDiscoverylinkByDescription(String description) throws DataAccessException {

		return findDiscoverylinkByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Discoverylink> findDiscoverylinkByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDiscoverylinkByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Discoverylink>(query.getResultList());
	}

	@Transactional
	public Set<Discoverylink> findDiscoverylinkByDescriptionContaining(String description) throws DataAccessException {

		return findDiscoverylinkByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Discoverylink> findDiscoverylinkByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDiscoverylinkByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Discoverylink>(query.getResultList());
	}

	@Transactional
	public Discoverylink findDiscoverylinkByPrimaryKey(Integer id) throws DataAccessException {
		return findDiscoverylinkByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Discoverylink findDiscoverylinkByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDiscoverylinkByPrimaryKey", startResult, maxRows, id);
			return (Discoverylink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Discoverylink> findDiscoverylinksByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDiscoverylinksByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Discoverylink> findAllDiscoverylinks() throws DataAccessException {

		return findAllDiscoverylinks(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Discoverylink> findAllDiscoverylinks(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDiscoverylinks", startResult, maxRows);
		return new LinkedHashSet<Discoverylink>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Discoverylink> findDiscoverylinkByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Discoverylink>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Discoverylink> findDiscoverylinksByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Discoverylink.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Discoverylink> discoverylinks = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return discoverylinks;
	}

	@Transactional
	public Discoverylink findDiscoverylinkByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findDiscoverylinkByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Discoverylink findDiscoverylinkByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDiscoverylinkByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Discoverylink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Discoverylink entity) {
		return true;
	}
}
